package br.com.caelum.vraptor.controller;

import java.util.List;

import javax.inject.Inject;

import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.auth_annotations.Administrador;
import br.com.caelum.vraptor.components.ProfessorSessioned;
import br.com.caelum.vraptor.dao.GenericDAO;
import br.com.caelum.vraptor.dao.ProfessorDAO;
import br.com.caelum.vraptor.entidades.Professor;

@Controller
public class ProfessorController {
	

	@Inject
    private Result result;
    
    @Inject
    private GenericDAO<Professor> dao;
    @Inject
    private ProfessorDAO professorDAO;
    @Inject
    private ProfessorSessioned professorSessioned;
    
    public void add(){
            
    }
    @br.com.caelum.vraptor.auth_annotations.Professor
    @Administrador
    public List<Professor> list(){
            return dao.find();
    }
   @Administrador
   @br.com.caelum.vraptor.auth_annotations.Professor
    public void save(Professor professor){
            dao.save(professor);
            result.redirectTo(this).list();
    }
   @br.com.caelum.vraptor.auth_annotations.Professor
   @Administrador
    public Professor edit(int id){
        return dao.find(id);
    }
   @Administrador
    public void delete(int id){
    	Professor professor= new Professor();
        professor.setId(id);
        dao.delete(professor);
        result.redirectTo(this).list();
    }
    
    public void login(String matricula, String senha){
    	Professor prof = professorDAO.find(matricula, senha);
        professorSessioned.login(prof);
        if(professorSessioned.isLogado()){
            result.redirectTo(IndexController.class).home();;
            }
        else{result.redirectTo(IndexController.class).index();}
    }
           
    
    public void logout(){
            professorSessioned.logout();
            result.redirectTo(IndexController.class).index();
    }

	
}
